
package com.yimixian.app.order;

import com.yimixian.app.R;
import com.yimixian.app.SystemFramework;
import com.yimixian.app.model.ValidatedOrder;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

import java.util.List;

public class FruitCouponView extends LinearLayout {

    @InjectView(R.id.tv_fruit_coupon_left)
    TextView mTvFruitCouponLeft;
    @InjectView(R.id.rl_no_choose_fruit_coupon)
    RelativeLayout mNoChooseLayout;
    @InjectView(R.id.back_image)
    ImageView mBackImage;
    @InjectView(R.id.rl_select_fruit_coupon)
    RelativeLayout mSelectLayout;

    @InjectView(R.id.iv_arrow)
    ImageView mIvArrow;
    @InjectView(R.id.tv_fruit_coupon_num)
    TextView mTvFruitCouponNum;

    @InjectView(R.id.tv_fruit_coupon_yuan)
    TextView mTvFruitCouponYuan;
    @InjectView(R.id.tv_fruit_coupon_price)
    TextView mTvFruitCouponPrice;
    @InjectView(R.id.tv_fruit_coupon_content)
    TextView mTvFruitCouponContent;

    public FruitCouponView(Context context) {
        super(context);
        initView();
    }

    public FruitCouponView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public FruitCouponView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        setOrientation(VERTICAL);
        LayoutInflater.from(getContext()).inflate(R.layout.fruit_coupon_view, this);
        ButterKnife.inject(this);
    }

    public void setNoFruitCouponSelect(List<ValidatedOrder.Bonus> list) {
        mNoChooseLayout.setVisibility(View.VISIBLE);
        mSelectLayout.setVisibility(View.GONE);
        if (list == null || list.size() == 0) {
            mTvFruitCouponLeft.setText(SystemFramework.getInstance().getGlobalContext()
                .getString(R.string.no_fruit_coupon));
            mTvFruitCouponLeft.setTextColor(getResources().getColor(R.color.confirm_order_grey));
            mIvArrow.setVisibility(View.GONE);
            mTvFruitCouponNum.setVisibility(View.GONE);
        } else {
            int useableCount=0;
            for(int i=0;i<list.size();i++){
                if (list.get(i).isUsable()){
                    useableCount++;
                }
            }
            mTvFruitCouponLeft.setText(String.format(SystemFramework.getInstance()
                    .getGlobalContext().getString(R.string.fruit_coupon_count), useableCount));
            mIvArrow.setVisibility(View.VISIBLE);
            mTvFruitCouponNum.setVisibility(View.VISIBLE);
            mTvFruitCouponNum.setText(useableCount+ "张");
        }
    }

    public void setFruitCouponSelect(ValidatedOrder.Bonus entity) {
        mNoChooseLayout.setVisibility(View.GONE);
        mSelectLayout.setVisibility(View.VISIBLE);
        mTvFruitCouponPrice.setText(entity.getDiscount() + "");
        mTvFruitCouponContent.setText(entity.getTitle() + "");

    }

    void setHideBackImage(){
        mBackImage.setVisibility(View.INVISIBLE);
        mIvArrow.setVisibility(View.INVISIBLE);
    }
}
